package com.vf.cloud.paas.app.master.api.product.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.product.service.IProductClassify;
import com.vf.cloud.paas.app.master.common.domain.ProductClassify;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/product/classify")
public class ProductClassifyController {

	@Autowired
	private IProductClassify productClassifyImpl;

	@RLog(value = "异步获取组件组树",type = "user")
	@TokenRequired
	@RequestMapping(value = "/lazy", method = RequestMethod.POST)
	public R<List<ProductClassify>> lazy(@RequestBody ProductClassify param) {
		return R.ok(productClassifyImpl.getChildrens(param));
	}
	
	@RLog(value = "获取组件组树",type = "user")
	@TokenRequired
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<ProductClassify>> list(@RequestBody ProductClassify param) {
		return R.ok(productClassifyImpl.getChildren(param));
	}
	

	@RLog(value = "保持组件组",type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ProductClassify> save(@RequestBody ProductClassify productClassify) {

		if (StrKit.isBlank(productClassify.getId())) {
			if (StrKit.isBlank(productClassify.getPid()))
				productClassify.setPid("-1");
			productClassify.setId(UuidUtil.getUUID());
			if (productClassify.save())
				return R.ok(productClassify);
		} else {
			if (productClassify.update())
				return R.ok(productClassify);
		}
		return R.failed();
	}

	@RLog(value = "删除组件组",type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody ProductClassify productClassify) {
		try {
			if (productClassify == null || StrKit.isBlank(productClassify.getId())) {
				return R.failed("参数必填.");
			}
			productClassifyImpl.delete(productClassify.getId());
			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
